package name.wilbur.course.db.bigone.dao.impl;

import java.sql.SQLException;

import name.wilbur.course.db.bigone.dao.BaseDAO;
import name.wilbur.course.db.bigone.dao.DAOFactory;
import name.wilbur.course.db.bigone.model.Profile;
import name.wilbur.course.db.bigone.util.Checker;


/**
 * @author wilbur
 */
public class ProfileDAO extends BaseDAO implements IProfileDAO {

	public ProfileDAO() {
		super(Profile.class);
	}
	
	public Profile findProfileByUserID(int id) {
		UserDAO userDao = (UserDAO) DAOFactory.createDAO(UserDAO.class);
		String sql = "SELECT ProfileID FROM " + table + " WHERE UserID = " + id;
		int profileID;
		try {
			userDao.queryNotClose(sql);
			if (userDao.getResultSet().next()) {
				profileID = userDao.getResultSet().getInt(1);
				return (Profile) findByPK(profileID);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			userDao.closeConnection();
			closeConnection();
		}
		return null;
	}
	
	public Profile[] findByNameAndCity (String name, int cityID) {
		String where_clause = "CityID = '" + cityID + "'";
		if (Checker.notEmptyString(name))
			where_clause += " AND Name = '" + name + "'";
		Profile[] profiles = (Profile[]) findWhere(where_clause);
		return profiles;
	}
}
